package com.zhangtai.utils.Excel.office;



import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.StringUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;

public class ExcelOffice implements MSOffice <SheetDO>{
	static {
		//日期转换
		ConvertUtils.register(new DateConvert(), Date.class);
		ConvertUtils.register(new IntegerConvert(), Integer.class);
	}

	


	private static final Logger logger = LoggerFactory.getLogger(ExcelOffice.class);

	//DO对应导出字段
	public final static Map<String, LinkedHashMap <String, String>> MAP_DO_FIELDS = new HashMap<String, LinkedHashMap <String, String>>();

	//默认第一个工作薄
	public final static int DEFAULT_SHEET_INDEX = 0;
	//第一行表头
//	public final static int DEFAULT_HEAD_INDEX = 0;
	//第二行开始表体
//	public final static int DEFAULT_BODY_INDEX = 1;
	//导入最大500条数
	public int IMPORT_MAX_ROWS = 1000;

	public final static ExcelOffice excelOffice = new ExcelOffice();

	//  private Workbook wb;
	private HSSFWorkbook wb;
	private ExcelOffice(){}

	public static ExcelOffice getInstance(){
		return excelOffice;
	}

	public void setMaxRows(int maxRows){
		IMPORT_MAX_ROWS = maxRows;
	}

	private void newWorkbook(InputStream is,boolean isXlsx) throws IOException{
		if (is != null) {
			try {
				if(isXlsx){
					//.xlsx后缀文件
					//wb = new XSSFWorkbook(is);
					wb = new HSSFWorkbook(is);
				}else{
//    				.xls 后缀文件
					wb = new HSSFWorkbook(is);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(is != null){
					is.close();
				}
			}

		} else {
			wb = new HSSFWorkbook();
		}
	}


	@Override
	public <A> List<A> read(File excelFile, Class<A> clazz) {
		try {
			if (excelFile.isDirectory() || !excelFile.exists()) {
				return null;
			}
			return read(new FileInputStream(excelFile),true, clazz);
		} catch (Exception e) {
		}
		return null;
	}

	@Override
	public <A> List<A> read(InputStream is,boolean isXlsx, Class<A> clazz) {
		List<A> data = new ArrayList<A>();
		SheetDO sheet = read(is,isXlsx);
		if (CollectionUtils.isEmpty(sheet.getHeadlist()) || CollectionUtils.isEmpty(sheet.getBodyList())) {
			return null;
		}
		LinkedHashMap <String, String> fieldMap =_getFieldOfClass(clazz);
		if (fieldMap.isEmpty()) {
			return null;
		}
		Map<String, String> map = new HashMap<String, String>();
		Map<Integer, String> importFields = new HashMap<Integer, String>();
		for (Iterator<Entry<String,String>> iter = fieldMap.entrySet().iterator(); iter.hasNext() ;) {
			Entry<String,String> entry = iter.next();
			map.put(entry.getValue(), entry.getKey());
		}
		for (int i= 0 ; i < sheet.getHeadlist().size() ; i++) {
			String code = map.get(sheet.getHeadlist().get(i));
			if (code == null) {
				continue;
			}
			importFields.put(i, code);
		}

		List<List<Object>> bodyList = sheet.getBodyList();
		for (List<Object> row : bodyList) {
			A a = _createObject(row, importFields, clazz);
			data.add(a);
		}
		return data;
	}

	@SuppressWarnings("unchecked")
	private <A> A _createObject(List<Object> row, Map<Integer, String> map, Class<A> clazz){
		Map<String, String> vals = new HashMap<String, String>();
		for (Iterator<Entry<Integer, String>> iter = map.entrySet().iterator(); iter.hasNext() ;) {
			Entry<Integer, String> entry = iter.next();
			try {
				String str = (String) row.get(entry.getKey());
				if (str != null) {
					vals.put(entry.getValue(), str);
				}
			} catch (Exception e) {
			}
		}

		Object obj = org.springframework.beans.BeanUtils.instantiateClass(clazz);
		BeanUtils.copyProperties(obj,vals);
		return (A) obj;
	}

	@Override
	public SheetDO read(InputStream is,boolean isXlsx) {
		SheetDO sheetDO = new SheetDO();
		try {
			newWorkbook(is,isXlsx);
			Sheet sheet = this.wb.getSheetAt(DEFAULT_SHEET_INDEX);
			_getHead(sheet, sheetDO);
			_getBody(sheet, sheetDO);
		} catch (Exception e) {
			logger.error("读取EXCEL失败", e);
		} finally {
			close();
		}
		return sheetDO;
	}

	private void _getHead(Sheet sheet, SheetDO sheetDO){
		Row row = sheet.getRow(sheetDO.getHeadIndex());
		sheetDO.setHeadlist(_getCellOfHead(row, null));
	}

	private void _getBody(Sheet sheet, SheetDO sheetDO) {
		int idx = sheetDO.getBodyIndex();
		Row row = null;
		do {
			row = sheet.getRow(idx);
			if (row == null) {
				return;
			}
			sheetDO.addRow(_getCellOfRow(row, sheetDO.getColumns()));
			idx ++;
		} while(row != null && idx <= IMPORT_MAX_ROWS);

	}

	private List<String> _getCellOfHead(Row row, Integer heads){
		List<String> cols = new ArrayList<String>();
		if (heads == null) {
			for (Iterator<Cell> iter = row.iterator(); iter.hasNext();) {
				Cell cell = iter.next();
				cols.add(cell.toString());
			}
		} else {//表体
			boolean isRowValid = false;
			for (int i = 0; i < heads; i++) {
				String temp = null;
				Cell cell = row.getCell(i);
				if (cell == null) {
					temp = "";
				} else {
					temp = _getCellOfVal(cell);
				}
				isRowValid |= StringUtils.isNotEmpty(temp);
				cols.add(temp);
			}
			if (!isRowValid) {
				return null;
			}
		}
		return cols;
	}

	private List<Object> _getCellOfRow(Row row, Integer heads){
		List<Object> cols = new ArrayList<Object>();
		if (heads == null) {
			for (Iterator<Cell> iter = row.iterator(); iter.hasNext();) {
				Cell cell = iter.next();
				cols.add(cell.toString());
			}
		} else {//表体
			boolean isRowValid = false;
			for (int i = 0; i < heads; i++) {
				String temp = null;
				Cell cell = row.getCell(i);
				if (cell == null) {
					temp = "";
				} else {
					temp = _getCellOfVal(cell);
				}
				isRowValid |= StringUtils.isNotEmpty(temp);
				cols.add(temp);
			}
			if (!isRowValid) {
				return null;
			}
		}
		return cols;
	}

	@SuppressWarnings("static-access")
	private String _getCellOfVal(Cell cell){
		if (cell == null) {
			return "";
		}
		if (cell.getCellType() == cell.CELL_TYPE_STRING) {
			return cell.getStringCellValue();
		} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC && DateUtil.isCellDateFormatted(cell)){
			//HSSFDateUtil.isCellDateFormatted(cell)
			return cell.getDateCellValue().getTime() + "";
		} else {
			cell.setCellType(Cell.CELL_TYPE_STRING);
			return cell.toString();
		}
	}

	@Override
	public boolean write(List<?> list, Class<?> clazz, OutputStream outputStream) {
		SheetDO sheetDO = _convertList2Sheet(list , clazz);
		return write(sheetDO, outputStream);
	}

	private LinkedHashMap <String, String> _getFieldOfClass(Class<?> clazz){
		if (clazz == null) {
			return null;
		}
		String doKey = clazz.getName();
		LinkedHashMap <String, String> fieldMap = MAP_DO_FIELDS.get(doKey);
		if (fieldMap == null) {
			fieldMap = _getFieldName(clazz);
			MAP_DO_FIELDS.put(doKey, fieldMap);
		}
		return fieldMap;
	}

	private SheetDO _convertList2Sheet(List<?> list, Class<?> clazz) {
		SheetDO sheetDO = new SheetDO();
		LinkedHashMap <String, String> fieldMap =_getFieldOfClass(clazz);
		if (fieldMap.isEmpty()) {
			return null;
		}
		sheetDO.addHead(fieldMap.values());
		if (CollectionUtils.isEmpty(list)) {
			return sheetDO;
		}
		List<List<Object>> bodyList = new ArrayList<List<Object>>();
		for (Object o : list) {
			List<Object> row = _getObjectVal(o, new ArrayList<String>(fieldMap.keySet()));
			bodyList.add(row);
		}
		sheetDO.setBodyList(bodyList);
		return sheetDO;
	}

	private List<Object> _getObjectVal(Object obj, List<String> fields){
		List<Object> val = new ArrayList<Object>();
		Class<?> clazz = obj.getClass();
		for (String field : fields) {
			try {
				PropertyDescriptor pd = new PropertyDescriptor(field,clazz);
				Method getMethod = pd.getReadMethod();
				Object o = getMethod.invoke(obj);
				val.add(_convert2Val(o));
			} catch (Exception e) {
				logger.info("error in convert field to String val.", e);
			}
		}
		return val;
	}

	private Object _convert2Val(Object o) {
		if (o == null) {
			return "";
		}
		if (o instanceof Date) {
			return DateUtils.format((Date) o,"yyyy-MM-dd HH:mm:ss");
		}
		return o;
	}

	private LinkedHashMap <String, String> _getFieldName(Class<?> clazz){
		LinkedHashMap <String, String> map = new LinkedHashMap<String, String>();
		Field[] flds = clazz.getDeclaredFields();
		for (Field f : flds){
			Excel excel = f.getAnnotation(Excel.class);
			if (excel == null) {
				continue;
			}
			String cn = excel.value();
			if (cn == null || cn.length() < 1){
				cn = f.getName();
			}
			map.put(f.getName(), cn);
		}
		return map;
	}

	@Override
	public boolean write(SheetDO sheetDO, OutputStream outputStream) {
		if (sheetDO == null || outputStream == null) {
			return false;
		}
		try {
			newWorkbook(null,true);
			String sheetName = sheetDO.getSheetName();
			if (sheetName == null || sheetName.trim().length() < 1) {
				sheetName =   DateUtils.format(new Date(),"yyyyMMddHHmmss");
			}
			HSSFSheet sheet = this.wb.createSheet(sheetName);
			HSSFCellStyle cellStyle = _setBodyStlye();
			HSSFCellStyle headCellStyle = _setHeadStlye();
			_setFirst(sheet, sheetDO, cellStyle);
			_setHead(sheet, sheetDO, headCellStyle);
			_setBody(sheet, sheetDO, cellStyle);
			_setLast(sheet, sheetDO, cellStyle);
			for (int i = 1; i < sheetDO.getBodyList().size()-1; i++){
				for (int j = 0; j < sheetDO.getHeadlist().size(); j++) {
					HSSFRow row= sheet.getRow(i);
					Cell cell=row.getCell(j);
					Object ret = null;
					Object ret0=null;
		    		HSSFRow row0= sheet.getRow(0);
		    		ret0 = row0.getCell(j).getStringCellValue().toString();
					if (null!=cell){
						switch (cell.getCellType()) {
				    	 case Cell.CELL_TYPE_BLANK:
					    	 ret = "";
					    	 break;
				    	 case Cell.CELL_TYPE_BOOLEAN:
					    	 ret = String.valueOf(cell.getBooleanCellValue());
					    	 break;
				    	 case Cell.CELL_TYPE_ERROR:
				    		 ret = null;
				    		 break;
				    	 case Cell.CELL_TYPE_FORMULA:
				    		 ret=cell.getCellFormula();
				    		 break;
				    	 case Cell.CELL_TYPE_NUMERIC:
				    	 if (DateUtil.isCellDateFormatted(cell)) {
						    	 Date theDate = cell.getDateCellValue();
						    	 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						    	 ret = formatter.format(theDate);
					    	 } else {
					    		 ret =cell.getNumericCellValue();
					    	 }
				    	 	break;
				    	 case Cell.CELL_TYPE_STRING:
				    		 ret = cell.getRichStringCellValue().getString();
				    	 	break;
				    	 default:
				    		 ret = ret0;
				    	 }
						if (ret.toString().length()>ret0.toString().length()){
							sheet.setColumnWidth(j,ret.toString().getBytes().length*2*120);
						} else {
							sheet.setColumnWidth(j,ret0.toString().getBytes().length*2*120);
						}
					}
				}
			}
			this.wb.write(outputStream);
		} catch (Exception e) {
			logger.error("写入EXCEL失败", e);
		} finally {
			close();
		}
		return false;
	}

	private void _setFirst(HSSFSheet sheet, SheetDO sheetDO, HSSFCellStyle cellStyle) {
		if (sheetDO.getFirstList() == null || sheetDO.getFirstList().isEmpty()) {
			return ;
		}
		for (int i = 0 ; i < sheetDO.getFirstList().size() ; i++ ){
			Row row = sheet.createRow(i);
			_setCell(row, sheetDO.getFirstList().get(i), cellStyle);
		}
	}

	private void _setHead(HSSFSheet sheet, SheetDO sheetDO, HSSFCellStyle cellStyle) {
		if (sheetDO.getHeadlist() == null || sheetDO.getHeadlist().isEmpty()) {
			return ;
		}
		Row row = sheet.createRow(sheetDO.getHeadIndex());
		row.setHeight((short)400);
		_setCellHead(row, sheetDO.getHeadlist(), cellStyle);
	}

	private void _setBody(HSSFSheet sheet, SheetDO sheetDO, HSSFCellStyle cellStyle) {
		if (sheetDO.getBodyList() == null || sheetDO.getBodyList().isEmpty()) {
			return ;
		}
		for (int i = 0 ; i < sheetDO.getBodyList().size() ; i++ ){
			Row row = sheet.createRow(sheetDO.getBodyIndex() + i);
			_setCell(row, sheetDO.getBodyList().get(i), cellStyle);
		}
	}

	private void _setLast(HSSFSheet sheet, SheetDO sheetDO, HSSFCellStyle cellStyle) {
		if (sheetDO.getLastList() == null || sheetDO.getLastList().isEmpty()) {
			return ;
		}
		int startIndex = sheetDO.getBodyIndex() + sheetDO.getBodyList().size();
		for (int i = 0 ; i < sheetDO.getLastList().size() ; i++ ){
			Row row = sheet.createRow(startIndex + i);
			_setCell(row, sheetDO.getLastList().get(i), cellStyle);
		}
	}



	private void _setCellHead(Row row, List<String> headlist, HSSFCellStyle cellStyle) {
		for (int i = 0 ; i < headlist.size() ; i++ ) {
			Cell cell = row.createCell(i);
			cell.setCellStyle(cellStyle);
			cell.setCellValue(headlist.get(i));
		}
	}

	private void _setCell(Row row, List<Object> bodylist, HSSFCellStyle cellStyle) {
		for (int i = 0 ; i < bodylist.size() ; i++ ) {
			Cell cell = row.createCell(i);
			cell.setCellStyle(cellStyle);
			if(bodylist.get(i) instanceof Integer){
				int num = (Integer) bodylist.get(i);
				Double donum = (double) num;
				cell.setCellValue(donum);
			}else if(bodylist.get(i) instanceof Double){
				cell.setCellValue((Double) bodylist.get(i));
			}else{
				cell.setCellValue((String) bodylist.get(i));
			}

		}
	}



	@Override
	public boolean close() {
		if (wb != null) {
			wb = null ;
		}
		return false;
	}

	public boolean writeWithRule(SheetDO sheetDO, OutputStream outputStream, Integer photoClom, Map<Integer,String> map,String imgPath) {
		if (sheetDO == null || outputStream == null) {
			return false;
		}
		try {
			newWorkbook(null, false);
			String sheetName = sheetDO.getSheetName();
			if (sheetName == null || sheetName.trim().length() < 1) {
				sheetName = DateUtils.format(new Date(),"yyyyMMddHHmmss");
			}
			HSSFSheet sheet = this.wb.createSheet(sheetName);
			HSSFCellStyle cellStyle=_setBodyStlye();
			HSSFCellStyle headCellStyle = _setHeadStlye();
			_setFirst(sheet, sheetDO, cellStyle);
			_setHead(sheet, sheetDO, headCellStyle);
			//有图片导出
			if(photoClom!=null){
				HSSFPatriarch patri = sheet.createDrawingPatriarch();
				_setBodyWithPhoto(patri,sheet, sheetDO, photoClom.intValue(),cellStyle, imgPath);
			}
			//无图片导出
			if (photoClom == null) {
				_setBody(sheet, sheetDO, cellStyle);
			}
			//有合并规则
			if (map!= null) {
				_setMergeRule(sheet,sheetDO,map);
			}
			_setLast(sheet, sheetDO,cellStyle);
			for (int i = 1; i < sheetDO.getBodyList().size()-1; i++){
				for (int j = 0; j < sheetDO.getHeadlist().size(); j++) {
					HSSFRow row= sheet.getRow(i);
					Cell cell=row.getCell(j);
					Object ret = null;
					Object ret0=null;
		    		HSSFRow row0= sheet.getRow(0);
		    		ret0 = row0.getCell(j).getStringCellValue().toString();
					if (null!=cell){
						switch (cell.getCellType()) {
					    	 case Cell.CELL_TYPE_BLANK:
						    	 ret = "";
						    	 break;
					    	 case Cell.CELL_TYPE_BOOLEAN:
						    	 ret = String.valueOf(cell.getBooleanCellValue());
						    	 break;
					    	 case Cell.CELL_TYPE_ERROR:
					    		 ret = null;
					    		 break;
					    	 case Cell.CELL_TYPE_FORMULA:
					    		 ret=cell.getCellFormula();
					    		 break;
					    	 case Cell.CELL_TYPE_NUMERIC:
					    	 if (DateUtil.isCellDateFormatted(cell)) {
							    	 Date theDate = cell.getDateCellValue();
							    	 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							    	 ret = formatter.format(theDate);
						    	 } else {
						    		 ret =cell.getNumericCellValue();
						    	 }
					    	 	break;
					    	 case Cell.CELL_TYPE_STRING:
					    		 ret = cell.getRichStringCellValue().getString();
					    	 	break;
					    	 default:
					    		 ret=ret0;
				    	 }
						if (ret.toString().length()>ret0.toString().length()){
							sheet.setColumnWidth(j,ret.toString().getBytes().length*2*150);
						} else {
							sheet.setColumnWidth(j,ret0.toString().getBytes().length*2*150);
						}
					}
				}
			}
			this.wb.write(outputStream);
		} catch (Exception e) {
			logger.error("写入EXCEL失败", e);
		} finally {
			close();
		}
		return false;

	}

	private HSSFCellStyle _setBodyStlye() {
		HSSFFont font = this.wb.createFont();
		font.setFontHeightInPoints((short) 9); //字体高度
		font.setColor(HSSFFont.COLOR_NORMAL); //字体颜色
		font.setFontName("宋体"); //字体
		//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
		font.setItalic(false); //是否使用斜体
		// font.setStrikeout(true); //是否使用划线
		// 设置单元格类型
		HSSFCellStyle cellStyle = this.wb.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局：居中
		cellStyle.setWrapText(true);
		return cellStyle;
	}

	private HSSFCellStyle _setHeadStlye() {
		HSSFFont font = this.wb.createFont();
		font.setFontHeightInPoints((short) 10); //字体高度
		font.setColor(HSSFFont.COLOR_NORMAL); //字体颜色
		font.setFontName("宋体"); //字体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
		font.setItalic(false); //是否使用斜体
		// font.setStrikeout(true); //是否使用划线
		// 设置单元格类型
		HSSFCellStyle cellStyle = this.wb.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局：居中
		cellStyle.setWrapText(true);
		return cellStyle;
	}


	private void _setMergeRule(HSSFSheet sheet, SheetDO sheetDO, Map<Integer,String> map) {
		for (Integer key : map.keySet()) {
			String value= map.get(key);
			String[] kk = value.split(",");
			//根据给定列值比较前后行结果后，再确定是否合并
			if (key!=null){
				for (int i = 0; i < sheetDO.getBodyList().size(); i++) {
					String baseStr = (String) sheetDO.getBodyList().get(i).get(key);
					String nextStr = "";
					if (i + 1 < sheetDO.getBodyList().size()) {
						nextStr = (String) sheetDO.getBodyList().get(i + 1).get(key);
					}
					if (baseStr.equals(nextStr)) {
						int y1 = sheetDO.getBodyIndex() + i;//开始行
						int y2 = sheetDO.getBodyIndex() + i + 1;//结束行
						if (kk.length > 0) {
							for (int j = 0; j < kk.length; j++) {
								String[] len= kk[j].split(":");
								int clonStar = Integer.parseInt(len[1]);
								int clonEnd = Integer.parseInt(len[3]);
								CellRangeAddress region = new CellRangeAddress(y1, y2, clonStar, clonEnd);//开始行，结束行，开始列，结束列
								sheet.addMergedRegion(region);
							}
						}

					}
				}
			}
			//指定行列合并
			else {
				if (kk.length > 0) {
					for (int j = 0; j < kk.length; j++) {
						String[] len = kk[j].split(":");
						int rowStar = Integer.parseInt(len[0]);
						int clonStar = Integer.parseInt(len[1]);
						int rowEnd = Integer.parseInt(len[2]);
						int clonEnd = Integer.parseInt(len[3]);
						CellRangeAddress region = new CellRangeAddress(rowStar, rowEnd, clonStar, clonEnd);//开始行，结束行，开始列，结束列
						sheet.addMergedRegion(region);
					}
				}

			}
		}

	}

	private void _setBodyWithPhoto(HSSFPatriarch patri, HSSFSheet sheet, SheetDO sheetDO, int photoClom , HSSFCellStyle cellStyle, String imgPath) {
		if (sheetDO.getBodyList() == null || sheetDO.getBodyList().isEmpty()) {
			return;
		}
		for (int i = 0; i < sheetDO.getBodyList().size(); i++) {
			Row row = sheet.createRow(sheetDO.getBodyIndex() + i);
			row.setHeight((short)500);
			_setCellWithPhoto(cellStyle,patri,row, sheetDO.getBodyList().get(i), photoClom, sheetDO.getBodyIndex() + i,imgPath);
		}
	}

	private void _setCellWithPhoto(HSSFCellStyle cellStyle, HSSFPatriarch patri, Row row, List<Object> bodylist, int photoClom, int r, String imgPath) {
		try{
			for (int i = 0; i < bodylist.size(); i++) {
				Cell cell = row.createCell(i);
				cell.setCellStyle(cellStyle);
				if (photoClom != i) {
					if (bodylist.get(i) instanceof Integer) {
						int num = (Integer) bodylist.get(i);
						Double donum = (double) num;
						cell.setCellValue(donum);
					} else if (bodylist.get(i) instanceof Double) {
						cell.setCellValue((Double) bodylist.get(i));
					} else {
						cell.setCellValue((String) bodylist.get(i));
					}
				}
				else {
					ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
					String photopath = (String) bodylist.get(i);
					String localPath= imgPath + photopath.split("/")[1];
					File file= new File(localPath);
					if (file.exists()){
						BufferedImage bufferImg = ImageIO.read(file);
						ImageIO.write(bufferImg, "JPG", byteArrayOut);
						//设置图片大小，位置
						HSSFClientAnchor anchor = new HSSFClientAnchor(0,0, 800, 255, (short) photoClom, r, (short) photoClom, r);//列，行；列，行
						anchor.setAnchorType(2);
						patri.createPicture(anchor, this.wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
						file.delete();
					}
				}
			}

		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	

}
